package com.crm.model.dto;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.common.Dictionary;
import com.crm.model.entity.contractManager.CustomerContract;
import com.crm.model.entity.customerManager.Customer;
import com.crm.model.entity.customerManager.CustomerProductDetail;
import com.crm.model.entity.customerManager.CustomerProductDetailMemberCard;
import com.crm.model.entity.customerManager.CustomerProductDetailThroughTrainTax;
import com.crm.model.entity.finishOrderProduct.FinishOrderProduct;
import com.crm.model.entity.productManager.CustomerMemberProduct;
import com.crm.model.entity.sys.ImportFailureLog;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.vo.orderManager.CustomerProductOrderVO;
import com.crm.model.vo.sys.ImportFailureLogVO;
import com.crm.model.vo.systemManager.MemberShipCardImportVO;
import com.crm.service.common.DictionaryDbService;
import com.crm.service.contractManager.CustomerContractDbService;
import com.crm.service.customerManager.CustomerDbService;
import com.crm.service.customerManager.CustomerProductDetailMemberCardDbService;
import com.crm.service.orderManager.CustomerProductOrderDbService;
import com.crm.service.productManager.CustomerMemberProductDbService;
import com.crm.service.returnMoney.FinishOrderDBservice;
import com.crm.service.sys.ImportFailureLogDbService;
import com.crm.service.sys.UserAccountDbService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

/**
 * @Author:baijian
 * @Date: 2021/6/26 9:19
 */
@Slf4j
public class InsertMemberCardCallable implements Callable<JSONObject> {



    private CustomerDbService customerDbService;
    private UserAccountDbService userAccountDbService;
    private CustomerContractDbService customerContractDbService;
    private CustomerProductOrderDbService customerProductOrderDbService;
    private CustomerProductDetailMemberCardDbService customerProductDetailMemberCardDbService;
    private FinishOrderDBservice finishOrderDBservice;
    private CustomerMemberProductDbService customerMemberProductDbService;
    private DictionaryDbService dictionaryDbService;
    private ImportFailureLogDbService importFailureLogDbService;
    private List<MemberShipCardImportVO> list;
    private CountDownLatch begin;
    private CountDownLatch end;
    private String companyId;

    public InsertMemberCardCallable(CustomerDbService customerDbService, UserAccountDbService userAccountDbService,
                                    CustomerContractDbService customerContractDbService,CustomerProductOrderDbService customerProductOrderDbService,
                                    CustomerProductDetailMemberCardDbService customerProductDetailMemberCardDbService,
                                    FinishOrderDBservice finishOrderDBservice,
                                    CustomerMemberProductDbService customerMemberProductDbService,
                                    DictionaryDbService dictionaryDbService,
                                    ImportFailureLogDbService importFailureLogDbService,
                                    List<MemberShipCardImportVO> list, CountDownLatch begin, CountDownLatch end, String companyId) {
        this.customerDbService = customerDbService;
        this.userAccountDbService = userAccountDbService;
        this.customerContractDbService = customerContractDbService;
        this.customerProductOrderDbService = customerProductOrderDbService;
        this.customerProductDetailMemberCardDbService = customerProductDetailMemberCardDbService;
        this.finishOrderDBservice = finishOrderDBservice;
        this.customerMemberProductDbService = customerMemberProductDbService;
        this.dictionaryDbService = dictionaryDbService;
        this.importFailureLogDbService = importFailureLogDbService;
        this.list = list;
        this.begin = begin;
        this.end = end;
        this.companyId = companyId;
    }

    @SneakyThrows
    @Override
    public JSONObject call() {
        //导入数量
        int importNum = 0;
        //重复的行数
        List<Integer> repeatIdStr = new ArrayList();
        //失败行数
        List<Integer> importFailIdStr = new ArrayList();
        JSONObject jsonObject = new JSONObject();
        UserAccount user = (UserAccount) SecurityUtils.getSubject().getPrincipal();

        for (int i = 0; i < list.size(); i++) {
            MemberShipCardImportVO importVO = list.get(i);
            UserAccount userAccount = null;
            FinishOrderProduct finishOrderProduct = null;
            CustomerProductOrderVO customerProductOrderVO = null;
            CustomerContract customerContract = null;

            try {
                if(StringUtils.isBlank(importVO.getMemberCardNumber())) {
                    if(userAccount == null) {
                        insertImportFailureLog(importVO, 2, "会员卡编号为空", "",companyId,user.getAccountId());
                    }

                    importFailIdStr.add(i+2);
                    continue;
                }

                CustomerProductDetailMemberCard memberCard = customerProductDetailMemberCardDbService.getCustomerProductDetailMemberCard(importVO.getMemberCardNumber());
                if(memberCard == null) {
                    //查询账号是否存在
                    Customer customer = null;
                    if (!StringUtils.isBlank(importVO.getAccountName())) {
                        userAccount = userAccountDbService.selectUserAccountByUserNameAndCompanyId(importVO.getAccountName(),companyId);
                    }
                    //else {
                        //insertImportFailureLog(importVO, 2, "所有人姓名为空", "",companyId,"");
                        //importFailIdStr.add(i+2);
                        //continue;
                    //}

                    if (userAccount != null) {
                        if (!StringUtils.isBlank(importVO.getCustomerName())) {
                            customer = customerDbService.selectCustomerNameAndAccountId(userAccount.getAccountId(), importVO.getCustomerName());
                        }
                        //else {
                            //insertImportFailureLog(importVO, 2, "客户姓名不存在", "",companyId,userAccount.getAccountId());
                            //importFailIdStr.add(i+2);
                            //continue;
                        //}
                        if (!StringUtils.isBlank(customer.getCustomerId()) && customer != null) {
                            if(!StringUtils.isBlank(importVO.getContractNumber())) {
                                customerContract = customerContractDbService.selectContractByContractNumber(importVO.getContractNumber());
                                if(customerContract !=null) {
                                    if(!StringUtils.isBlank(importVO.getOrderNumber())){
                                        customerProductOrderVO = customerProductOrderDbService.getOrderInfoByNumber(importVO.getOrderNumber());
                                        if(!StringUtils.isBlank(customerProductOrderVO.getProductOrderId())) {
                                            //查询会员卡是否存在
                                            CustomerProductDetailMemberCard customerProductDetailMemberCard = customerProductDetailMemberCardDbService.selectCustomerProductDetailMemberCard(importVO.getMemberCardNumber(),userAccount.getAccountId(),customer.getCustomerId(),customerContract.getContractId(),customerProductOrderVO.getProductOrderId(),companyId);
                                            if(customerProductDetailMemberCard == null) {

                                                if("是".equals(importVO.getIsItValid())) {
                                                    //通过订单id和产品分类 会员 1查询产品信息
                                                    List<FinishOrderProduct> finishOrderProductList = finishOrderDBservice.selectFinishOrderProductList("1","1", customerProductOrderVO.getProductOrderId());
                                                    if(finishOrderProductList != null && finishOrderProductList.size() > 0) {
                                                        finishOrderProduct = finishOrderProductList.get(0);
                                                    /*Boolean bs = addMemberCard(importVO,finishOrderProduct,companyId,customer,customerContract,customerProductOrderVO,userAccount);
                                                    if (bs) {
                                                        importNum = importNum + 1;
                                                    } else {
                                                        //insertImportFailureLog(importVO, 2, "导入异常", "",companyId,userAccount.getAccountId());
                                                        //importFailIdStr.add(i+2);
                                                        //continue;
                                                    }*/
                                                    }else{
                                                        //insertImportFailureLog(importVO, 2, "订单完成的产品表信息不存在", "",companyId,userAccount.getAccountId());
                                                        //importFailIdStr.add(i+2);
                                                        //continue;
                                                    }
                                                }else{
                                                    //insertImportFailureLog(importVO, 2, "会员卡没有在有效期", "",companyId,userAccount.getAccountId());
                                                    //importFailIdStr.add(i+2);
                                                    //continue;
                                                }
                                            }//else{
                                                //insertImportFailureLog(importVO, 1, "会员卡号已存在", customerProductDetailMemberCard.getCustomerProductDetailMemberCardId(),companyId,userAccount.getAccountId());
                                                //importFailIdStr.add(i+2);
                                                //continue;
                                            //}
                                        }else{
                                            //insertImportFailureLog(importVO, 2, "订单不存在", "",companyId,userAccount.getAccountId());
                                            //importFailIdStr.add(i+2);
                                            //continue;
                                        }
                                    }else{
                                        //insertImportFailureLog(importVO, 2, "订单编号为空", "",companyId,userAccount.getAccountId());
                                        //importFailIdStr.add(i+2);
                                        //continue;
                                    }
                                }else{
                                    //insertImportFailureLog(importVO, 2, "合同不存在", "",companyId,userAccount.getAccountId());
                                    //importFailIdStr.add(i+2);
                                    //continue;
                                }
                            }else{
                                //insertImportFailureLog(importVO, 2, "合同编号为空", "",companyId,userAccount.getAccountId());
                                //importFailIdStr.add(i+2);
                                //continue;
                            }
                        } else {
                            //insertImportFailureLog(importVO, 2, "客户不存在", "",companyId,userAccount.getAccountId());
                            //importFailIdStr.add(i+2);
                            //continue;
                        }

                    } else {
                        //导入失败
                        //insertImportFailureLog(importVO, 2, "所有人在表中没有", "",companyId,"");
                        //importFailIdStr.add(i+2);
                        //continue;
                    }

                    Boolean bs = addMemberCard(importVO,finishOrderProduct,companyId,customer,customerContract,customerProductOrderVO,userAccount);
                    if(bs){
                        importNum = importNum + 1;
                    }
                }else {
                    repeatIdStr.add(i+2);
                }


            } catch (Exception e) {
                if(userAccount == null) {
                    insertImportFailureLog(importVO, 2, "导入异常", "",companyId,user.getAccountId());
                }else{
                    insertImportFailureLog(importVO, 2, "导入异常", "",companyId,userAccount.getAccountId());
                }

                importFailIdStr.add(i+2);
                log.error("导入会员卡异常, Exception info : {}", e);
            }
        }

        jsonObject.put("importNum",importNum);
        jsonObject.put("repeatIdStr",repeatIdStr);
        jsonObject.put("importFailIdStr",importFailIdStr);
        return jsonObject;
    }

    public Boolean addMemberCard(MemberShipCardImportVO importVO,FinishOrderProduct finishOrderProduct,String companyId,Customer customer,
                                 CustomerContract customerContract,CustomerProductOrderVO customerProductOrderVO,UserAccount userAccount) throws ParseException {
        UserAccount user = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        Date startTm = formatTime(importVO.getStartTm());
        Date endTm = formatTime(importVO.getEndTm());
        Date createTm = formatTimeSf(importVO.getCreateTm());
        String customerProductDetailMemberCardId = CommonUtils.getGUID();
        CustomerProductDetailMemberCard customerProductDetailMemberCard = new CustomerProductDetailMemberCard();
        customerProductDetailMemberCard.setCustomerProductDetailMemberCardId(customerProductDetailMemberCardId);


        if(userAccount != null) {
            customerProductDetailMemberCard.setCreatePeopleId(userAccount.getAccountId());
            customerProductDetailMemberCard.setUpdatePeopleId(userAccount.getAccountId());
            customerProductDetailMemberCard.setAccountId(userAccount.getAccountId());
        }else{
            customerProductDetailMemberCard.setMemberErrorData(1);
        }
        if(finishOrderProduct != null) {
            customerProductDetailMemberCard.setMemberProductCode(finishOrderProduct.getProductCode());
            customerProductDetailMemberCard.setFinishOrderProductNum(finishOrderProduct.getFinishOrderProductNum());
        }else{
            customerProductDetailMemberCard.setMemberErrorData(1);

        }
        if(customer !=null) {
            customerProductDetailMemberCard.setCustomerId(customer.getCustomerId());
        }else{
            customerProductDetailMemberCard.setMemberErrorData(1);
        }
        if(customerContract !=null) {
            customerProductDetailMemberCard.setContractId(customerContract.getContractId());
            customerProductDetailMemberCard.setContractNumber(customerContract.getContractNumber());
        }else{
            customerProductDetailMemberCard.setMemberErrorData(1);
        }
        if(customerProductOrderVO != null) {
            customerProductDetailMemberCard.setProductOrderId(customerProductOrderVO.getProductOrderId());
            customerProductDetailMemberCard.setOrderNumber(customerProductOrderVO.getOrderNumber());
        }else{
            customerProductDetailMemberCard.setMemberErrorData(1);
        }

        customerProductDetailMemberCard.setMemberCardNumber(importVO.getMemberCardNumber());
        customerProductDetailMemberCard.setMemberCardPassword(importVO.getMemberCardPassword());
        customerProductDetailMemberCard.setStartTm(startTm);
        customerProductDetailMemberCard.setEndTm(endTm);
        if(!StringUtils.isBlank(importVO.getProductPrice())) {
            customerProductDetailMemberCard.setProductPrice(new BigDecimal(importVO.getProductPrice()));
        }else{
            customerProductDetailMemberCard.setProductPrice(new BigDecimal(0));
        }
        customerProductDetailMemberCard.setCreateTm(createTm);
        customerProductDetailMemberCard.setUpdateTm(new Date());
        customerProductDetailMemberCard.setCompanyId(companyId);
        customerProductDetailMemberCard.setPhone(importVO.getPhone());
        customerProductDetailMemberCard.setBz(importVO.getBz());
        customerProductDetailMemberCard.setDataSource(1);
        Boolean result= customerDbService.addCustomerProductDetailMemberCard(customerProductDetailMemberCard);

        if(result) {
            //插入税法直通车数据
            if(!StringUtils.isBlank(importVO.getTrainTax())) {
                List<CustomerProductDetailThroughTrainTax> list = trainTaxSub(importVO.getTrainTax());
                if(list != null && list.size() > 0) {
                    for(CustomerProductDetailThroughTrainTax customerProductDetailThroughTrainTax : list) {
                        customerProductDetailThroughTrainTax.setThroughTrainTaxId(CommonUtils.getGUID());
                        if(userAccount == null) {
                            customerProductDetailThroughTrainTax.setCreatePeopleId(user.getAccountId());
                            customerProductDetailThroughTrainTax.setUpdatePeopleId(user.getAccountId());
                        }else{
                            customerProductDetailThroughTrainTax.setCreatePeopleId(userAccount.getAccountId());
                            customerProductDetailThroughTrainTax.setUpdatePeopleId(userAccount.getAccountId());
                        }

                        customerProductDetailThroughTrainTax.setCreateTm(createTm);

                        customerProductDetailThroughTrainTax.setUpdateTm(new Date());
                        customerProductDetailThroughTrainTax.setCustomerProductDetailMemberCardId(customerProductDetailMemberCardId);
                        customerProductDetailThroughTrainTax.setDataSource(1);
                        customerProductDetailThroughTrainTax.setCompanyId(companyId);
                        customerDbService.addCustomerProductDetailThroughTrainTax(customerProductDetailThroughTrainTax);
                    }
                }
            }
        }


        /*if(result && userAccount != null && finishOrderProduct != null && customer !=null && customerContract !=null && customerProductOrderVO != null) {

            CustomerMemberProduct customerMemberProduct = customerMemberProductDbService.queryMemberProductCode(finishOrderProduct.getProductCode(),Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
            if(!StringUtils.isBlank(customerMemberProduct.getMemberProductId())) {

                //现场参会
                if(!StringUtils.isBlank(importVO.getSiteParticipation())) {
                    CustomerProductDetail customerProductDetail = addCustomerProductDetail(customerMemberProduct, finishOrderProduct, createTm, userAccount, customer,companyId);
                    String courseNum = String.valueOf(new Date().getTime())+0+1;
                    customerProductDetail.setCourseNum(courseNum);
                    customerProductDetail.setServiceType("2");  //1:线上课程；2:线下课程；3:增值服务
                    String courseTypeId = addDictionary("现场参会",userAccount);
                    customerProductDetail.setCourseTypeId(courseTypeId);
                    customerProductDetail.setServiceCount(importVO.getSiteParticipation());
                    customerDbService.insertCustomerProductDetail(customerProductDetail);
                }

                //专行会交流
                if(!StringUtils.isBlank(importVO.getProfessionalExchange())) {
                    CustomerProductDetail customerProductDetail = addCustomerProductDetail(customerMemberProduct, finishOrderProduct, createTm, userAccount, customer,companyId);
                    String courseNum = String.valueOf(new Date().getTime())+0+2;
                    customerProductDetail.setCourseNum(courseNum);
                    customerProductDetail.setServiceType("2");
                    String courseTypeId = addDictionary("专行会交流",userAccount);
                    customerProductDetail.setCourseTypeId(courseTypeId);
                    customerProductDetail.setServiceCount(importVO.getProfessionalExchange());
                    customerDbService.insertCustomerProductDetail(customerProductDetail);
                }

                //网课直播次数
                if(!StringUtils.isBlank(importVO.getDirectSeedingNumber())) {
                    CustomerProductDetail customerProductDetail = addCustomerProductDetail(customerMemberProduct, finishOrderProduct, createTm, userAccount, customer,companyId);
                    String courseNum = String.valueOf(new Date().getTime())+0+3;
                    customerProductDetail.setCourseNum(courseNum);
                    customerProductDetail.setServiceType("1");
                    String courseTypeId = addDictionary("网课直播次数",userAccount);
                    customerProductDetail.setCourseTypeId(courseTypeId);
                    customerProductDetail.setServiceCount(importVO.getDirectSeedingNumber());
                    customerDbService.insertCustomerProductDetail(customerProductDetail);
                }

                //子账号个数
                if(!StringUtils.isBlank(importVO.getNumberOfSubAccounts())) {
                    CustomerProductDetail customerProductDetail = addCustomerProductDetail(customerMemberProduct, finishOrderProduct, createTm, userAccount, customer,companyId);
                    String courseNum = String.valueOf(new Date().getTime())+0+4;
                    customerProductDetail.setCourseNum(courseNum);
                    customerProductDetail.setServiceType("3");
                    String courseTypeId = addDictionary("子账号个数",userAccount);
                    customerProductDetail.setCourseTypeId(courseTypeId);
                    customerProductDetail.setServiceCount(importVO.getNumberOfSubAccounts());
                    customerDbService.insertCustomerProductDetail(customerProductDetail);
                }

                if(StringUtils.isBlank(importVO.getSiteParticipation()) && StringUtils.isBlank(importVO.getProfessionalExchange())
                && StringUtils.isBlank(importVO.getDirectSeedingNumber()) && StringUtils.isBlank(importVO.getNumberOfSubAccounts())){
                    CustomerProductDetail customerProductDetail = addCustomerProductDetail(customerMemberProduct, finishOrderProduct, createTm, userAccount, customer,companyId);
                    customerDbService.insertCustomerProductDetail(customerProductDetail);
                }
            }
        }*/
        return result;
    }

    /**
     * 插入字典表
     * @param dictName 字典名称
     * @return
     */
    public String addDictionary(String dictName,UserAccount userAccount) {
        List<Dictionary> list = dictionaryDbService.selectDictionaryByTypeCode("15");
        String dictId = "";
        Dictionary dict = dictionaryDbService.selectDictionaryByDicNameAndDictId("15", dictName);
        if(dict == null) {
            Dictionary dictionary = new Dictionary();
            dictId = CommonUtils.getGUID();
            dictionary.setDictId(dictId);
            dictionary.setTypeCode("15");
            int size = list.size()+1;
            dictionary.setDictCode(String.valueOf(size));
            dictionary.setDictName(dictName);
            dictionary.setCreateTm(new Date());
            dictionary.setCreatePeopleId(userAccount.getAccountId());
            dictionaryDbService.insertDictionary(dictionary);
        }else {
           dictId = dict.getDictId();
        }

        return dictId;
    }

    public CustomerProductDetail addCustomerProductDetail(CustomerMemberProduct customerMemberProduct,FinishOrderProduct finishOrderProduct,Date createTm,UserAccount userAccount,Customer customer,String companyId) {
        CustomerProductDetail customerProductDetail = new CustomerProductDetail();
        //插入会员产品明细
        customerProductDetail.setCustomerProductDetailId(CommonUtils.getGUID());
        customerProductDetail.setMemberProductCode(customerMemberProduct.getMemberProductCode());
        customerProductDetail.setFinishOrderProductNum(finishOrderProduct.getFinishOrderProductNum());
        customerProductDetail.setMemberCardName(customerMemberProduct.getMemberCardName());
        customerProductDetail.setSellingCardPrice(customerMemberProduct.getSellingCardPrice());
        customerProductDetail.setServiceNumber(customerMemberProduct.getServiceNumber());
        customerProductDetail.setProductMembershipLevel(customerMemberProduct.getProductMembershipLevel());
        customerProductDetail.setCreateTm(createTm);
        customerProductDetail.setCreatePeopleId(userAccount.getAccountId());
        customerProductDetail.setUpdateTm(new Date());
        customerProductDetail.setUpdatePeopleId(userAccount.getAccountId());
        customerProductDetail.setAccountId(userAccount.getAccountId());
        customerProductDetail.setCustomerId(customer.getCustomerId());
        customerProductDetail.setDataSource(1);
        customerProductDetail.setCompanyId(companyId);
        return customerProductDetail;
    }

    public static void main(String[] args) {
        List<CustomerProductDetailThroughTrainTax> list = trainTaxSub("用户名:1517953\t密码:17782218;\n" +
                "用户名:1517954\t密码:99819250");
        for(CustomerProductDetailThroughTrainTax tax : list) {
            System.out.println("卡号:"+tax.getMemberCardNumber() + " 密码:" + tax.getMemberCardPassword());
            System.out.println("-----------------------------");
        }
    }

    /**
     * 税法直通车 切割参数
     * @param trainTax
     * @return
     */
    public static List<CustomerProductDetailThroughTrainTax> trainTaxSub(String trainTax) {
        List<CustomerProductDetailThroughTrainTax> list = new ArrayList<>();
        if(trainTax.contains("卡号:")) {
            if(trainTax.contains(";")) {
                String[] split = trainTax.split(";");
                for(String a : split) {
                    CustomerProductDetailThroughTrainTax tax = new CustomerProductDetailThroughTrainTax();
                    String num = a.substring(a.indexOf("卡号:")+3,a.indexOf("密码:"));
                    String password = a.substring(a.indexOf("密码:")+3,a.length());
                    tax.setMemberCardNumber(num);
                    tax.setMemberCardPassword(password);
                    list.add(tax);
                }
            }else{
                CustomerProductDetailThroughTrainTax tax = new CustomerProductDetailThroughTrainTax();
                String num = trainTax.substring(trainTax.indexOf("卡号:")+3,trainTax.indexOf("密码:"));
                String password = trainTax.substring(trainTax.indexOf("密码:")+3,trainTax.length());
                tax.setMemberCardNumber(num);
                tax.setMemberCardPassword(password);
                list.add(tax);
            }
        } else if(trainTax.contains("用户名：")) {
            trainTax = trainTax.replaceAll("\t|\n","");
            if(trainTax.indexOf("用户名：",2) != -1){
                String[] split = trainTax.split("用户名：");
                for(String a : split) {
                    if(!StringUtils.isBlank(a)) {
                        CustomerProductDetailThroughTrainTax tax = new CustomerProductDetailThroughTrainTax();
                        String num = a.substring(0,a.indexOf("密码"));
                        String password = a.substring(a.indexOf("密码")+3,a.length());
                        tax.setMemberCardNumber(num);
                        tax.setMemberCardPassword(password);
                        list.add(tax);
                    }
                }
            }else{
                CustomerProductDetailThroughTrainTax tax = new CustomerProductDetailThroughTrainTax();
                String num = trainTax.substring(4,trainTax.indexOf("密码"));
                String password = trainTax.substring(trainTax.indexOf("密码")+3,trainTax.length());
                tax.setMemberCardNumber(num);
                tax.setMemberCardPassword(password);
                list.add(tax);
            }
        } else if(!trainTax.contains("卡号:") && !trainTax.contains("用户名：")) {
            if(trainTax.contains(";")) {
                String[] split = trainTax.split(";");
                for(String a : split) {
                    CustomerProductDetailThroughTrainTax tax = new CustomerProductDetailThroughTrainTax();
                    String num = a.substring(0,a.indexOf(":"));
                    String password = a.substring(a.indexOf(":")+1,a.length());
                    tax.setMemberCardNumber(num);
                    tax.setMemberCardPassword(password);
                    list.add(tax);
                }
            }else{
                CustomerProductDetailThroughTrainTax tax = new CustomerProductDetailThroughTrainTax();
                String num = trainTax.substring(0,trainTax.indexOf(":"));
                String password = trainTax.substring(trainTax.indexOf(":")+1,trainTax.length());
                tax.setMemberCardNumber(num);
                tax.setMemberCardPassword(password);
                list.add(tax);
            }
        } else {
            CustomerProductDetailThroughTrainTax tax = new CustomerProductDetailThroughTrainTax();
            tax.setMemberCardNumber(trainTax);
            list.add(tax);
        }




        return list;
    }

    /**
     * 记录导入失败日志
     * @param importVO
     * @param type
     */
    public void insertImportFailureLog(MemberShipCardImportVO importVO,Integer type,String content,String customerProductDetailMemberCardId,String companyId,String accountId){
        //SysCompany sysCompany = sysCompanyDbService.selectSysCompanyById(companyId);
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        ImportFailureLog importFailureLog = new ImportFailureLog();
        List<ImportFailureLogVO> list = importFailureLogDbService.selectImportFailureLogByNameAndType(importVO.getMemberCardNumber(),9,companyId,accountId);
        if(list == null || list.size() == 0){
            if (type == 1){
                if(!StringUtils.isBlank(customerProductDetailMemberCardId)) {
                    importFailureLog.setDataId(customerProductDetailMemberCardId);
                }
            }
            importFailureLog.setImportFailureLogId(CommonUtils.getGUID());
            importFailureLog.setOperatePeopleId(userAccount.getAccountId());
            importFailureLog.setOperateTime(new Date());
            importFailureLog.setOperateType("9");
            importFailureLog.setImportName(importVO.getMemberCardNumber());
            importFailureLog.setReasonForFailure(content);
            importFailureLog.setImportData(JSONObject.toJSONString(importVO));
            importFailureLog.setLogType(String.valueOf(type));
            //importFailureLog.setExceptionLog();
            importFailureLog.setCompanyId(companyId);
            importFailureLog.setAccountId(accountId);
            importFailureLogDbService.insertImportFailureLog(importFailureLog);
        }
    }



    /**
     * 转时间格式
     * @param date 创建时间  结束时间
     * @return
     */
    public Date formatTime(String date) throws ParseException {
        //同一个Excel有不一样的时间格式……
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.parse(date);
    }

    /**
     * 转时间格式
     * @param date 创建时间  结束时间
     * @return
     */
    public Date formatTimeSf(String date) throws ParseException {
        //同一个Excel有不一样的时间格式……
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return simpleDateFormat.parse(date);
    }

    /*public static void main(String[] args) {
        String a = "武汉东湖新技术开发区高新二路388号光谷国际生物医药企业加速器一期工程1号厂房C1-1-0283自贸区武汉片区";
        //String[] split = a.split(",");
        System.out.println(a.length());
    }*/
}
